package com.comsoftstar.nt5s.Activity;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hzc.nt5s.IConstant;
import com.hzc.nt5s.R;
import com.hzc.nt5s.adapter.RegulationsDataAdapter;
import com.hzc.nt5s.bean.Regulations;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/***
 *      5s 条例档案
 *      2017-8-17
 * */
public class RegulationsActivity extends AppCompatActivity implements View.OnClickListener {


    private Context mContext;
    private ListView listView;
    private ImageView back,refresh;
    private ProgressDialog progressDialog;
    private RegulationsDataAdapter regulationsDataAdapter;
    private EditText et_regulations;
    private List<Regulations> mList;
    private List<Regulations> mCacheList;
    private Gson gson;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regulations);
        mContext = RegulationsActivity.this;
        initView();
        initData();
        getDataFromNet();
    }
    private void initView() {
        back=(ImageView)findViewById(R.id.iv_back);
        refresh=(ImageView)findViewById(R.id.iv_refresh);
        progressDialog = new ProgressDialog(mContext);
        progressDialog.setMessage("正在获取数据，请稍候");
        listView=(ListView)findViewById(R.id.lv_regulations);
        et_regulations=(EditText)findViewById(R.id.et_regulations);
        et_regulations.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(mList == null || mCacheList == null){
                    return;
                }
                List<Regulations> list = new ArrayList<>();
                String etInput = et_regulations.getText().toString();

                if("".equals(etInput)){
                    mList.clear();
                    mList.addAll(mCacheList);
//                    System.out.println("----------------aaaaaaaaaaaaa" + mList.size());
//                    System.out.println("////////////////aaaaaaaaaaaaa" + mCacheList.size());
                }else {
                    for (Regulations rs : mCacheList) {
                        if (rs.getStandard().contains(etInput)) {//contains字符串包含對比
                            list.add(rs);
                        }
                    }
                    mList.clear();
                    mList.addAll(list);
//                    System.out.println("----------------bbbbbbbbbbbb" + mList.size());
//                    System.out.println("////////////////bbbbbbbbbbbb" + mCacheList.size());
                }
                regulationsDataAdapter.notifyDataSetChanged();
            }
        });
    }

    private void initData() {
        back.setOnClickListener(this);
        refresh.setOnClickListener(this);
        mList = new ArrayList<>();
        regulationsDataAdapter = new RegulationsDataAdapter(mContext, mList);
        listView.setAdapter(regulationsDataAdapter);
    }


    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (isShouldHideInput(v, ev)) {

                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
            }
            return super.dispatchTouchEvent(ev);
        }

        if (getWindow().superDispatchTouchEvent(ev)) {
            return true;
        }
        return onTouchEvent(ev);
    }
    public  boolean isShouldHideInput(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {
            int[] leftTop = { 0, 0 };
            v.getLocationInWindow(leftTop);
            int left = leftTop[0];
            int top = leftTop[1];
            int bottom = top + v.getHeight();
            int right = left + v.getWidth();
            if (event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom) {
                return false;
            } else {
                return true;
            }
        }
        return false;
    }
    private void getDataFromNet() {
        RequestParams params = new RequestParams(IConstant.URL + "getAllStandard");

//        System.out.println("请求的URL：" + params.toString());


        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                //解析result
                progressDialog.dismiss();
                gson = new Gson();
                List<Regulations> list = gson.fromJson(result, new TypeToken<ArrayList<Regulations>>(){}.getType());
                if(list == null){
                    return;
                }
                mCacheList = list;
                for(Regulations rs : list){
                    System.out.println(rs.getStandard()+rs.getCardNum()+rs.getRegulationNo());
                }
                mList.addAll(list);
                regulationsDataAdapter.notifyDataSetChanged();//更新数据
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                progressDialog.dismiss();
            }
            @Override
            public void onCancelled(CancelledException cex) {
                progressDialog.dismiss();
            }
            @Override
            public void onFinished() {
                progressDialog.dismiss();
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_refresh:
                et_regulations.setText("");
                getDataFromNet();
                break;
        }
    }
}

